package jnpf.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.DynDicUtil;
import jnpf.base.util.VisualUtils;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.entity.ZXGL_XiangMuBianGengEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.generater.util.GeneraterSwapUtil;
import jnpf.model.common.ZXGL_FlowWriteBack;
import jnpf.model.zxgl_xiangmubiangeng.*;
import jnpf.service.ZXGL_XiangMuBianGengService;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * ZXGL_XiangMuBianGeng
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-22
 */
@Slf4j
@RestController
@Api(tags = "项目变更", value = "zxgl")
@RequestMapping("/ZXGL_XiangMuBianGeng")
public class ZXGL_XiangMuBianGengController {


    @Autowired
    private DynDicUtil dynDicUtil;

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_XiangMuBianGengService zXGL_XiangMuBianGengService;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxglXiangMuLiXiangMingXiService;


    /**
     * 列表
     *
     * @param zXGL_XiangMuBianGengPagination
     * @return
     */
    @ApiOperation("项目变更分页")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_XiangMuBianGengPagination zXGL_XiangMuBianGengPagination) throws IOException {
        List<ZXGL_XiangMuBianGengEntity> list = zXGL_XiangMuBianGengService.getList(zXGL_XiangMuBianGengPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_XiangMuBianGengEntity entity : list) {
            entity.setBgr(generaterSwapUtil.userSelectValues(entity.getBgr()));
            entity.setBgbm(generaterSwapUtil.comSelectValues(entity.getBgbm()));
            entity.setBglx(dynDicUtil.getDicName(entity.getBglx(), "672043656068232837"));
            entity.setXmrwlx(dynDicUtil.getDicName(entity.getXmrwlx(), "670160918163781253"));
            entity.setBgqzt(dynDicUtil.getDicName(entity.getBgqzt(), "667276146122954053"));
            entity.setBghzt(dynDicUtil.getDicName(entity.getBghzt(), "667276146122954053"));
        }
        List<ZXGL_XiangMuBianGengListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_XiangMuBianGengListVO.class);
        for (ZXGL_XiangMuBianGengListVO zXGL_XiangMuBianGengVO : listVO) {
        }


        for (ZXGL_XiangMuBianGengListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_XiangMuBianGengPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_XiangMuBianGengCrForm
     * @return
     */
    @ApiOperation("添加项目变更信息")
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_XiangMuBianGengCrForm zXGL_XiangMuBianGengCrForm) throws DataException {
        String visualId = "667645796438514885";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();

        // reggion 赋值部门领导和负责人
        ZXGL_XiangMuLiXiang_MingXiEntity model = zxglXiangMuLiXiangMingXiService.getInfo(zXGL_XiangMuBianGengCrForm.getXmid());
        zXGL_XiangMuBianGengCrForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
        zXGL_XiangMuBianGengCrForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
        // endreion

        // region 赋值主责人是否为部门负责人
        log.info("entity:==========================>" + zXGL_XiangMuBianGengCrForm);
        if (StrUtil.isNotEmpty(zXGL_XiangMuBianGengCrForm.getDepartmentManagerZzbm()) && zXGL_XiangMuBianGengCrForm.getZrr().equals(zXGL_XiangMuBianGengCrForm.getDepartmentManagerZzbm())) {
            zXGL_XiangMuBianGengCrForm.setZzrsfbmfzr("669876724938339973"); // 是
        } else {
            zXGL_XiangMuBianGengCrForm.setZzrsfbmfzr("669876694466721413"); // 否
        }
        // endregion

        ZXGL_XiangMuBianGengEntity entity = JsonUtil.getJsonToBean(zXGL_XiangMuBianGengCrForm, ZXGL_XiangMuBianGengEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());
        entity.setMainid(zXGL_XiangMuBianGengCrForm.getMainid());


        zXGL_XiangMuBianGengService.save(entity);
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_XiangMuBianGengCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_XiangMuBianGengCrForm, userInfo, zXGL_XiangMuBianGengCrForm.getCandidateList());
                FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(mainId);
                if (flowTaskEntity != null) {
                    flowTaskEntity.setTitle(zXGL_XiangMuBianGengCrForm.getXmmc() + "-项目变更-" + userInfo.getUserName() + "-" + DateUtil.today());
                    flowTaskEntity.setDataTable("zxgl_xiangmubiangeng");
                    flowTaskService.update(flowTaskEntity);
                }
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("项目变更信息")
    @GetMapping("/{id}")
    public ActionResult<ZXGL_XiangMuBianGengInfoVO> info(@PathVariable("id") String id) {
        ZXGL_XiangMuBianGengEntity entity = zXGL_XiangMuBianGengService.getInfo(id);
        ZXGL_XiangMuBianGengInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_XiangMuBianGengInfoVO.class);

        //子表
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @ApiOperation("修改项目变更信息")
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_XiangMuBianGengUpForm zXGL_XiangMuBianGengUpForm) throws DataException {
        String visualId = "667645796438514885";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuBianGengEntity entity = zXGL_XiangMuBianGengService.getInfo(id);
        if (entity != null) {

            // reggion 赋值部门领导和负责人
            ZXGL_XiangMuLiXiang_MingXiEntity model = zxglXiangMuLiXiangMingXiService.getInfo(zXGL_XiangMuBianGengUpForm.getXmid());
            zXGL_XiangMuBianGengUpForm.setDepartmentManagerZzbm(model.getDepartmentManagerZzbm());
            zXGL_XiangMuBianGengUpForm.setDepartmentHeadleaderZzbm(model.getDepartmentHeadleaderZzbm());
            // endreion

            // region 赋值主责人是否为部门负责人
            if (StrUtil.isNotEmpty(zXGL_XiangMuBianGengUpForm.getDepartmentManagerZzbm()) && zXGL_XiangMuBianGengUpForm.getZrr().equals(zXGL_XiangMuBianGengUpForm.getDepartmentManagerZzbm())) {
                zXGL_XiangMuBianGengUpForm.setZzrsfbmfzr("669876724938339973"); // 是
            } else {
                zXGL_XiangMuBianGengUpForm.setZzrsfbmfzr("669876694466721413"); // 否
            }
            // endregion

            ZXGL_XiangMuBianGengEntity subentity = JsonUtil.getJsonToBean(zXGL_XiangMuBianGengUpForm, ZXGL_XiangMuBianGengEntity.class);


            zXGL_XiangMuBianGengService.update(id, subentity);

            if (FlowStatusEnum.submit.getMessage().equals(zXGL_XiangMuBianGengUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_XiangMuBianGengUpForm.getCandidateList());
                    FlowTaskEntity flowTaskEntity = flowTaskService.getInfo(id);
                    if (flowTaskEntity != null) {
                        flowTaskEntity.setTitle(zXGL_XiangMuBianGengUpForm.getXmmc() + "-项目变更-" + userInfo.getUserName() + "-" + DateUtil.today());
                        flowTaskService.update(flowTaskEntity);
                    }
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除项目变更信息")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_XiangMuBianGengEntity entity = zXGL_XiangMuBianGengService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_XiangMuBianGengService.delete(entity);

        }
        return ActionResult.success("删除成功");
    }


    /**
     * 更新项目变更
     *
     * @param flowWriteBack
     * @return
     */
    @ApiOperation("项目变更流程回调")
    @PostMapping("/updateFlow")
    @Transactional
    public ActionResult updateLX(@RequestBody ZXGL_FlowWriteBack flowWriteBack) throws DataException {
        boolean update = zXGL_XiangMuBianGengService.updateFlow(flowWriteBack.getTaskId());
        if (update) {
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }


    /**
     * 根据项目获取变更记录
     *
     * @param xmId
     * @return
     * @throws IOException
     */
    @ApiOperation("根据项目获取变更记录")
    @GetMapping("/getListByXmId/{xmId}")
    public ActionResult getListByXmId(@PathVariable String xmId) throws IOException {
        return ActionResult.success(zXGL_XiangMuBianGengService.getList(xmId));
    }

    @ApiOperation("获取项目变更信息")
    @GetMapping("/getBianGengById/{id}")
    public ActionResult getBianGengById(@PathVariable("id") String id) {
        return ActionResult.success(zXGL_XiangMuBianGengService.getBianGengById(id));
    }
}
